package com.zmzncs.shop.module.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.shop.common.pojo.PageInfo;
import com.zmzncs.shop.common.pojo.Rest;
import com.zmzncs.shop.common.util.RestUtil;
import com.zmzncs.shop.common.util.JsonUtil;
import com.zmzncs.shop.module.system.pojo.user.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.zmzncs.shop.module.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import javax.validation.constraints.NotNull;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-12-31
 */
@Api(tags = {""})
@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private UserService userService;


    @ApiOperation(value = "新增")
    @PostMapping("add")
    public Rest add(@RequestBody UserAddForm form){
        userService.add(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public Rest delete(@PathVariable Long id){
        userService.delete(id);

        return RestUtil.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping("update")
    public Rest update(@RequestBody UserUpdateForm form){
        userService.update(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "查询分页数据")
    @GetMapping("page")
    public Rest<PageInfo<UserVO>> page(UserSearchForm form){
        IPage<UserDTO> iPage = userService.page(form);
        PageInfo<UserVO> pageInfo = JsonUtil.pageTransform(iPage, UserVO.class);

        List<User> list = userService.list();
        List<UserVO> userVOList = JsonUtil.listTransform(list, UserVO.class);

        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("get-by-id")
    public Rest<UserVO> getById(@RequestParam Long id){
        UserDTO userDTO = userService.getById(id);

        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDTO, userVO);

        return RestUtil.success(userVO);
    }

}
